package com.link.base.base.buryingpoint.advertising.service;

import javax.annotation.Resource;

import com.link.base.base.buryingpoint.buriedcode.model.BuriedCode;
import com.link.base.base.buryingpoint.buriedcode.service.BuriedCodeService;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.util.StringUtils;
import com.link.base.base.buryingpoint.advertising.model.AdvertisingTracking;
import org.springframework.stereotype.Service;
import com.link.base.base.buryingpoint.advertising.dao.mybatis.mapper.AdvertisingTrackingMapper;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 广告跟踪-广告跟踪
 *
 * @author 秦学刚
 * @date 2019/02/26 14:44
 */
@Service
public class AdvertisingTrackingServiceImpl extends BasicServiceImpl<AdvertisingTracking> implements AdvertisingTrackingService {

    @Resource
    private AdvertisingTrackingMapper    advertisingTrackingMapper;
    @Resource
    private BuriedCodeService buriedCodeService;

    @Override
    public BasicMapper<AdvertisingTracking> getBasicMapper() {
        return advertisingTrackingMapper;
    }

    /**
     * 新建前处理
     * @author qinxuegang
     * @param entity
     * @throws Exception
     */

    @Override
    public void beforInsert(AdvertisingTracking entity) throws Exception {
        if (StringUtils.isBlank(entity.getStatus())) {
            entity.setStatus("Y");
        }
    }
    /**
     * 更新前处理
     * @author qinxuegang
     * @param entity
     * @throws Exception
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void beforUpdate(AdvertisingTracking entity) throws Exception {
        if (StringUtils.isBlank(entity.getStatus())) {
            throw new Exception("请传入广告状态");
        }
        if ("N".equalsIgnoreCase(entity.getStatus())) {
            // 将广告下的所有的埋点信息状态修改为失效
            QueryParams qps = new QueryParams();
            qps.setPageFlag(false);
            qps.addFilter(new Filter("advertisementId", Filter.OP_EQUAL, String.valueOf(entity.getId())));
            BuriedCode buriedCode = (BuriedCode) BasicModel.transformClass(BuriedCode.class, qps);
            List<BuriedCode> buriedCodeList = buriedCodeService.queryByExamplePage(buriedCode);
            for (BuriedCode tempBuriedCode : buriedCodeList) {
                tempBuriedCode.setStatus("N");
                buriedCodeService.update(tempBuriedCode);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void advertInvalid(AdvertisingTracking advertisingTracking) throws Exception {
        QueryParams qps = new QueryParams();
        qps.setPageFlag(false);
        qps.addFilter(new Filter("activityId", Filter.OP_EQUAL, advertisingTracking.getActivityId()));
        AdvertisingTracking advert = (AdvertisingTracking) BasicModel.transformClass(AdvertisingTracking.class, qps);
        List<AdvertisingTracking> advertList = this.queryByExamplePage(advert);
        for (AdvertisingTracking tempAdvert : advertList) {
            tempAdvert.setStatus("N");
            this.update(tempAdvert);
        }
    }
}

